//
//  BkGoodsMessageView.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/13.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//

#import "BkGoodsMessageView.h"


@interface BkGoodsMessageView()
@property (nonatomic, strong) UIView *bacView;

@property (nonatomic,strong) UIImageView *headerImgView;
@property (nonatomic,strong) UILabel *bkNameLabel;
@property (nonatomic,strong) UILabel *moneyLabel;
@property (nonatomic,strong) UIView *septorLine;
@property (nonatomic,strong) UILabel *amountNameLabel;
@property (nonatomic,strong) UIButton *addBtn;
@property (nonatomic,strong) UIButton *reduceBtn;
@property (nonatomic,strong) UILabel *amountLabel;


@property (nonatomic,strong) UILabel *sevenLabel;
@property (nonatomic,strong) UILabel *totalAmountLabel;




@end

@implementation BkGoodsMessageView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self viewConfig];
    }
    return self;
}

-(void)viewConfig
{
    self.frame =CGRectMake(0, 0, TSScreenWidth, TSScaleW(200));
    self.backgroundColor = kSubBacColor;
    [self addSubview:self.bacView];
    [self.bacView addSubview:self.headerImgView];
    [self.bacView addSubview:self.bkNameLabel];
    [self.bacView addSubview:self.sevenLabel];
    [self.bacView addSubview:self.moneyLabel];
    [self.bacView addSubview:self.addBtn];
    [self.bacView addSubview:self.amountLabel];
    [self.bacView addSubview:self.reduceBtn];
    [self.bacView addSubview:self.totalAmountLabel];
    
  
    self.height = self.bacView.bottom + TSScaleW(8);
    
}

-(UIView *)bacView{
    if (!_bacView) {
        _bacView = [[UIView alloc]initWithFrame:CGRectMake(0, TSScaleW(8), TSScreenWidth , TSScaleW(103))];
        _bacView.backgroundColor = kWhiteColor;
    }
    return _bacView;
}
-(UIImageView *)headerImgView{
    if (!_headerImgView) {
        _headerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(TSScaleW(11), TSScaleW(15) , TSScaleW(56), TSScaleW(75))];
        _headerImgView.backgroundColor = kGrayTxtColor;

    }
    return _headerImgView;
}
-(UILabel *)bkNameLabel{
    if (!_bkNameLabel) {
        _bkNameLabel = [TSWLTools allocLabel:@"初中数学运算大法" font:systemFont(TSScaleW(14)) textColor:kMainTxtColor frame:CGRectMake(_headerImgView.right + TSScaleW(10),   TSScaleW(5) + _headerImgView.top, TSScaleW(210), TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
        //_bkNameLabel.numberOfLines = 0;
    }
    return _bkNameLabel;
}
-(UILabel *)sevenLabel{
    if (!_sevenLabel) {
        _sevenLabel = [TSWLTools allocLabel:@"7天无理由退换" font:systemFont(TSScaleW(12)) textColor:kSubTxtColor frame:CGRectMake(_bkNameLabel.left, TSScaleW(9) + _bkNameLabel.bottom, TSScaleW(260), TSScaleW(12)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _sevenLabel;
}
-(UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel = [TSWLTools allocLabel:@"￥999.00" font:systemFont(TSScaleW(12)) textColor:kRedColor frame:CGRectMake(_headerImgView.right + TSScaleW(10), _sevenLabel.bottom + TSScaleW(20), TSScaleW(260), TSScaleW(15)) textAlignment:(NSTextAlignmentLeft)];
        NSString *string = _moneyLabel.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        [atts setAttributes:@{NSFontAttributeName:systemBoldFont(TSScaleW(15))} range:NSMakeRange(1,  string.length - 4)];
        _moneyLabel.attributedText = atts;
    }
    return _moneyLabel;
}


-(UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [TSWLTools allocButtonTitle:@"+" font:systemBoldFont(18) textColor:kMainTxtColor image:nil frame:CGRectMake(TSScreenWidth - TSScaleW(25), _sevenLabel.top, TSScaleW(18), TSScaleW(18)) sel:@selector(addAction:) taget:self];
        [_addBtn setTitleColor:kGrayTxtColor forState:(UIControlStateSelected)];
        
    }
    return _addBtn;
}
-(UILabel *)amountLabel{
    if (!_amountLabel) {
        _amountLabel = [TSWLTools allocLabel:@"1" font:systemFont(TSScaleW(12)) textColor:kMainTxtColor frame:CGRectMake(0, 0, TSScaleW(33), TSScaleW(17)) textAlignment:(NSTextAlignmentCenter)];
        _amountLabel.backgroundColor = kMainBgColor;
        _amountLabel.centerY = _addBtn.centerY;
        _amountLabel.right = _addBtn.left;
         
    }
    return _amountLabel;
}
-(UILabel *)totalAmountLabel{
    if (!_totalAmountLabel) {
        _totalAmountLabel = [TSWLTools allocLabel:@"x1" font:systemFont(TSScaleW(12)) textColor:kSubTxtColor frame:CGRectMake(0, _moneyLabel.top, TSScaleW(200), TSScaleW(12)) textAlignment:(NSTextAlignmentRight)];
        _totalAmountLabel.right = self.bacView.width - TSScaleW(12);
    }
    return _totalAmountLabel;
}
-(UIButton *)reduceBtn{
    if (!_reduceBtn) {
        _reduceBtn = [TSWLTools allocButtonTitle:@"-" font:systemBoldFont(18) textColor:kMainTxtColor image:nil frame:CGRectMake(0, _addBtn.top, TSScaleW(18), TSScaleW(18)) sel:@selector(reduceAction:) taget:self];
        [_reduceBtn setTitleColor:kGrayTxtColor forState:(UIControlStateSelected)];
        _reduceBtn.right = _amountLabel.left;
       
    }
    return _reduceBtn;
}
-(void)addAction:(UIButton *)sender{
    self.amountCount ++;
  
}
-(void)reduceAction:(UIButton *)sender
{
    if (self.amountCount<= 1) {
        self.amountCount = 1;
    }else{
        self.amountCount--;
    }
}
-(void)setAmountCount:(NSInteger)amountCount
{
    _amountCount = amountCount;
    
    _amountLabel.text = [NSString stringWithFormat:@"%ld",_amountCount];
//    if (_detailModel) {
//        double index  =  _detailModel.goodsprice1.floatValue *_amountCount;
//        _totalLabel.text = [NSString stringWithFormat:@"总计：￥%.2f",index];
//        _scoreLabel.text = [NSString stringWithFormat:@"%ld积分",_detailModel.goodsprice.integerValue *_amountCount];
//        !self.scoreAmountBlock?:self.scoreAmountBlock(_amountCount);
//    }
    _totalAmountLabel.text = [NSString stringWithFormat:@"x%ld",_amountCount];
    
    !self.changeAmountBlock?:self.changeAmountBlock(_amountCount);
    
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *gimage = _dataDic[@"gimage"];
    if (!gimage.length) {
        NSArray *array = _dataDic[@"images"];
        gimage = array.count?array.firstObject:@"";
    }
    [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:gimage]];
    self.bkNameLabel.text = _dataDic[@"gname"]?:_dataDic[@"name"];
    NSString *newprice = _dataDic[@"newprice"];
    if ([kIsmember intValue] == 0) {
        newprice = _dataDic[@"oldprice"];
    }
    _moneyLabel.text = [NSString stringWithFormat:@"￥%.2f",newprice.doubleValue];
    NSString *string = _moneyLabel.text;
    NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
    [atts setAttributes:@{NSFontAttributeName:systemBoldFont(TSScaleW(15))} range:NSMakeRange(1,  string.length - 4)];
    _moneyLabel.attributedText = atts;
    NSString *num = _dataDic[@"numbers"]?:@"1";
    _amountCount = num.integerValue;
    _amountLabel.text = [NSString stringWithFormat:@"%ld",_amountCount];

    _totalAmountLabel.text = [NSString stringWithFormat:@"x%ld",_amountCount];
}
-(void)setIsCar:(BOOL)isCar
{
    _isCar = isCar;
    if (_isCar) {
        _addBtn.hidden = _reduceBtn.hidden = YES;
    }
    
}
@end
